<template>
    <div>
        <!-- 面包屑导航区域-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>活动管理</el-breadcrumb-item>
          <el-breadcrumb-item>拓展活动列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片区域-->
        <el-card>
          <!-- 搜索与添加区域-->
        <el-row :gutter="20">
            <el-col :span="10">
              <el-input placeholder="请输入内容" v-model="queryInfo.kw" clearable @clear="getCourseList">
                <el-button slot="append" icon="el-icon-search" @click="getkw"></el-button>
            </el-input>
            </el-col>
            <el-col :span="4">
                <el-button type="primary" @click="addcoursedialog">添加课程</el-button>
            </el-col>
        </el-row>
          <!-- 用户列表区域 border:绘制表格边框线 stripe:隔行变色-->
        <el-table :data="courseList" border stripe>
            <el-table-column label="序列" type="index"></el-table-column>
            <el-table-column label="课程代码" prop="coursecode"></el-table-column>
            <el-table-column label="课程名称" prop="coursename"></el-table-column>
            <el-table-column label="授课老师" prop="teacher"></el-table-column>
            <el-table-column label="学时" prop="coursetime" width="60px"></el-table-column>
            <el-table-column label="学分" prop="courseGPA" width="60px"></el-table-column>
            <el-table-column label="课程类型" prop="coursetype"></el-table-column>
            <!-- <el-table-column label="课程ID" prop="typeid"></el-table-column> -->
            <el-table-column label="操作" width="180px">
              <template slot-scope="scope">
                <!-- 修改按钮-->
                <el-button type="primary" icon="el-icon-edit" size="mini" @click="showeditDialog(scope.row)"></el-button>
                <!-- 删除按钮-->
                <el-button type="danger" icon="el-icon-delete" size="mini" @click="removecourseById(scope.row._id)"></el-button>
              </template>
            </el-table-column>
        </el-table>
        <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.page"
      :page-sizes="[1, 2, 5, 8]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
        </el-card>
        <!-- 添加课程的对话框区域-->
        <el-dialog
          title="添加课程"
          :visible.sync="adddialogVisible"
          width="50%" @close="adddialogclosed">
          <el-form :model="addForm" :rules="addFormrules" ref="addFormRef" label-width="100px">
            <el-form-item label="课程代码" prop="coursecode">
              <el-input v-model="addForm.coursecode"></el-input>
            </el-form-item>
            <el-form-item label="课程名称" prop="coursename">
              <el-input v-model="addForm.coursename"></el-input>
            </el-form-item>
            <el-form-item label="授课老师" prop="teacher">
              <el-input v-model="addForm.teacher"></el-input>
            </el-form-item>
            <el-form-item label="学时" prop="coursetime">
              <el-input v-model="addForm.coursetime"></el-input>
            </el-form-item>
            <el-form-item label="学分" prop="courseGPA">
              <el-input v-model="addForm.courseGPA"></el-input>
            </el-form-item>
            <el-form-item label="课程类型" prop="coursetype">
              <!-- <el-input v-model="addForm.coursetype"></el-input> -->
              <el-select v-model="addForm.coursetype" placeholder="请选择">
                <el-option
                  v-for="item in coursetypeList"
                  :key="item.typeid"
                  :label="item.typename"
                  :value="item.typename">
                </el-option>
              </el-select>
            </el-form-item>
            <!-- <el-form-item label="类型id" prop="typeid">
              <el-select v-model="addForm.typeid" placeholder="请选择">
                <el-option
                  v-for="item in coursetypeList"
                  :key="item.typeid"
                  :label="item.typeid"
                  :value="item.typeid">
                </el-option>
              </el-select>
            </el-form-item> -->
          </el-form>
          <!-- 底部区域-->
          <span slot="footer" class="dialog-footer">
            <el-button @click="adddialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addcourse">确 定</el-button>
          </span>
        </el-dialog>
        <!-- 编辑用户的对话框区域-->
        <el-dialog
          title="修改课程"
          :visible.sync="editdialogVisible"
          width="50%"
          @close="editdialogclosed">
          <el-form :model="editForm" :rules="editFormrules" ref="editFormRef" label-width="100px">
            <el-form-item label="课程名称">
              <el-input v-model="editForm.coursename" disabled></el-input>
            </el-form-item>
            <el-form-item label="授课老师">
              <el-input v-model="editForm.teacher"></el-input>
            </el-form-item>
            <el-form-item label="课程学时" prop="coursetime">
              <el-input v-model="editForm.coursetime"></el-input>
            </el-form-item>
            <el-form-item label="课程学分" prop="courseGPA">
              <el-input v-model="editForm.courseGPA"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="editdialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="editcourseInfo">确 定</el-button>
          </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
  data() {
    return {
      // 获取课程列表的参数对象
      queryInfo: {
        page: 1,
        pagesize: 5,
        kw: ''
      },
      // 保存所有课程信息
      courseList: [],
      // 保存所有课程类型数据
      coursetypeList: [],
      total: 0,
      // 添加课程的表单数据
      addForm: {
        coursecode: '',
        coursename: '',
        teacher: '',
        coursetime: '',
        courseGPA: '',
        coursetype: '',
        typeid: ''
      },
      // 控制添加用户对话框的显示与隐藏
      adddialogVisible: false,
      // 添加表单的验证规则对象
      addFormrules: {
        coursecode: [
          { required: true, message: '请输入课程编码', trigger: 'blur' }
        ],
        coursename: [
          { required: true, message: '请输入课程名', trigger: 'blur' }
        ],
        teacher: [
          { required: true, message: '请输入授课老师', trigger: 'blur' },
          { min: 2, max: 8, message: '老师的名字在2~8个字符', trigger: 'blur' }
        ],
        coursetime: [
          { required: true, message: '请输入授课时长', trigger: 'blur' }
        ],
        courseGPA: [
          { required: true, message: '请输入课程学分', trigger: 'blur' }
        ],
        coursetype: [
          { required: true, message: '请输入课程类型', trigger: 'blur' }
        ],
        typeid: [
          { required: true, message: '请输入类型id', trigger: 'blur' }
        ]
      },
      // 修改表单的验证规则
      editFormrules: {
        teacher: [
          { required: true, message: '请输入授课老师', trigger: 'blur' },
          { min: 2, max: 8, message: '老师的名字在2~8个字符', trigger: 'blur' }
        ],
        coursetime: [
          { required: true, message: '请输入授课时长', trigger: 'blur' }
        ],
        courseGPA: [
          { required: true, message: '请输入课程学分', trigger: 'blur' }
        ]
      },
      // 控制修改课程对话框的显示与隐藏
      editdialogVisible: false,
      // 查询到的课程信息对象
      editForm: {}
    }
  },
  created() {
    // 获取所有课程
    this.getCourseList()
  },
  methods: {
    async getCourseList() {
      const { data: res } = await this.$http.post('/course/getInfoByPage', this.queryInfo)
      if (res.err === 0) {
        this.courseList = res.info.list
        this.total = res.info.count
      } else {
        return this.$message.error('获取课程列表失败')
      }
      console.log(res)
    },
    // 搜索（模糊）查询
    async getkw() {
      const { data: res } = await this.$http.post('/course/getInfoBykw', this.queryInfo)
      if (res.err === 0) {
        this.courseList = res.list
      } else {
        return this.$message.error('获取用户列表失败')
      }
      console.log(res)
    },
    // 监听pagesize改变事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getCourseList()
    },
    // 监听页码值改变事件
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.page = newPage
      this.getCourseList()
    },
    // 监听添加用户对话框的关闭事件
    adddialogclosed() {
      this.$refs.addFormRef.resetFields()
    },
    // 新增用户信息并提交事件
    addcourse() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        // 可以发起添加用户的请求
        const { data: res } = await this.$http.post('/course/add', this.addForm)
        if (res.err !== 0) {
          this.$message.error('添加用户失败')
        }
        this.$message.success('添加用户成功')
        // 隐藏添加用户对话框
        this.adddialogVisible = false
        // 重新加载数据
        this.getUserList()
      })
    },
    // 展示编辑用户的对话框(将信息显示到表单)
    async showeditDialog(id) {
      console.log(id)
      this.editForm = id
      this.editdialogVisible = true
    },
    // 监听修改用户对话框的关闭事件
    editdialogclosed() {
      this.$refs.editFormRef.resetFields()
    },
    // 修改用户信息并提交事件
    editcourseInfo() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        // 可以发起修改课程的请求
        console.log(this.editForm)
        const { data: res } = await this.$http.post('/course/update',
          {
            _id: this.editForm._id,
            teacher: this.editForm.teacher,
            coursetime: this.editForm.coursetime,
            courseGPA: this.editForm.courseGPA
          })
        if (res.err !== 0) {
          this.$message.error('修改课程失败')
        }
        this.$message.success('修改课程成功')
        // 隐藏修改课程对话框
        this.editdialogVisible = false
        // 重新加载数据
        this.getCourseList()
      })
    },
    // 根据Id删除用户
    async removecourseById(id) {
      const course = id
      console.log(course)
      // 弹窗询问用户是否删除数据
      const ref = await this.$confirm('此操作将永久删除该课程, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .catch(err => {
          return err
        })
      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel
      if (ref !== 'confirm') {
        return this.$message.info('取消删除')
      }
      const { data: res } = await this.$http.post('/course/del', { _id: course })
      if (res.err === 0) {
        this.$message.success('删除一条数据')
        this.getCourseList()
      } else {
        return this.$message.error('删除课程失败')
      }
    },
    // 添加课程按钮事件
    async addcoursedialog() {
      const { data: res } = await this.$http.post('/all/course')
      if (res.err === 0) {
        this.coursetypeList = res.list
      } else {
        return this.$message.error('获取用户列表失败')
      }
      console.log(res)
      this.adddialogVisible = true
    }
  }
}
</script>

<style lang="less" scoped>

</style>
